package com.cxyd.modules.pda.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.exception.RootException;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.api.dto.request.ContainerCallRequest;
import com.cxyd.modules.api.dto.request.ContainerRequest;
import com.cxyd.modules.api.dto.request.PurchaseDetailRequest;
import com.cxyd.modules.api.dto.request.TaskRequest;
import com.cxyd.modules.wms.dto.TaskDTO;
import com.cxyd.modules.wms.entity.PurchaseDetailEntity;
import com.cxyd.modules.wms.entity.TaskEntity;
import com.cxyd.modules.wms.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cxyd-pda")
public class RfController {
    private final PurchaseService purchaseService;
    private final PurchaseDetailService purchaseDetailService;
    private final TaskService taskService;
    private final ContainerService containerService;
    private final StockService stockService;

    public RfController(PurchaseService purchaseService, PurchaseDetailService purchaseDetailService, TaskService taskService, ContainerService containerService, StockService stockService) {
        this.purchaseService = purchaseService;
        this.purchaseDetailService = purchaseDetailService;
        this.taskService = taskService;
        this.containerService = containerService;
        this.stockService = stockService;
    }

    /**
     * 任务信息
     */
    @GetMapping("task-page")
    public Result<PageData<TaskDTO>> taskPage(@RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.rfPage(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }

    /**
     * 余料回库
     */
    @PostMapping("residual")
    @LogOperation("余料回库")
    public Result residual(@RequestBody Map<String, Object> params) {
        ProcResult result = purchaseService.residualBackRegister(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    @PostMapping("check-affirm")
    @LogOperation("盘点确认")
    public Result checkAffirm(@RequestBody TaskRequest request) {
//        if (null == request.getActlQty() || 0 == request.getActlQty()) {
//            throw new RootException("领料数量不能为空或者零！");
//        }
        if (null == request.getTaskId()) {
            throw new RootException("任务Id不能为空！");
        }
        return stockService.checkAffirm(request);
    }

    @PostMapping("empty-register")
    @LogOperation("空框登记")
    public Result emptyRegister(@RequestBody ContainerRequest request) {
        if (StringUtils.isBlank(request.getContNo())) {
            throw new RootException("托盘条码不能为空");
        }
//        不再对站台进行校验（防止人为选错）
//        if (StringUtils.isBlank(request.getStationNo())) {
//            throw new RootException("站台编号不能为空");
//        }
        containerService.register(request);
        return new Result().ok("空框[" + request.getContNo() + "]入库登记成功!等待小车接送");
    }

    @PostMapping("empty-call")
    @LogOperation("空框呼叫")
    public Result emptyCall(@RequestBody ContainerCallRequest request) {
        //功能说明：只需要根据站台就可知呼叫哪个库区（托盘库或料箱库）
        //1F_OUT_11 = 料箱库   1F_OUT_12 = 托盘库
        if (StringUtils.isBlank(request.getStationNo())) {
            throw new RootException("站台编号不能为空");
        }
        if (request.getPlanQty() <= 0) {
            throw new RootException("托盘数量不能小于等于0");
        }
        containerService.emptyContainerCall(request);
        return new Result().ok("空框呼叫成功!站台[" + request.getStationNo() + "]需求数量[" + request.getPlanQty() + "]");
    }

    /**
     * 入库单查询
     */
    @GetMapping("/detail/list")
    public Result purchaseDetailList(@RequestParam Map<String, Object> params) {
        String deliverNo = (String) params.get("deliverNo");
        if (com.cxyd.common.utils.StringUtils.isBlank(deliverNo)) {
            throw new RootException("收货单不能为空！");
        }
        List<PurchaseDetailEntity> purchaseDetailEntityList = purchaseDetailService.detailList(deliverNo);
        return new Result().ok(purchaseDetailEntityList);
    }

    /**
     * 入库登记
     */
    @PostMapping("/register")
    public Result purchaseRegister(@RequestBody PurchaseDetailRequest request) {
        if (null == request.getDetailId()) {
            throw new RootException("入库明细Id不能为空！");
        }
        if (null == request.getContNo()) {
            throw new RootException("托盘号不能为空！");
        }
        if (null == request.getSpuNo()) {
            throw new RootException("器材代码不能为空！");
        }
//        不使用入库站台区分（防止人为选错站台）,直接用容器的类型做参考
//        if (null == request.getStationNo()) {
//            throw new RootException("入库站台不能为空！");
//        }
        if (null == request.getNum() || request.getNum() <= 0) {
            throw new RootException("数量不能为空或者小于等于0！");
        }
        return purchaseDetailService.purchaseRegister(request);
    }

    /**
     * 入库放行
     */
    @PostMapping("/store")
    public Result store(@RequestBody PurchaseDetailRequest request) {
        if (null == request.getContNo()) {
            throw new RootException("托盘条码不能为空！");
        }
        purchaseDetailService.store(request);
        return new Result().ok("容器[" + request.getContNo() + "]成功放行!等待小车接送!");
    }

    /**
     * 出库任务查询(容器出库)
     */
    @GetMapping("/task/query")
    public Result store(@RequestParam Map<String, Object> params) {
        String contNo = (String) params.get("contNo");
//        新增任务子类型（更准确的匹配到要作业的任务）
        String taskSubType = (String) params.get("taskSubType");
        if (com.cxyd.common.utils.StringUtils.isBlank(contNo)) {
            throw new RootException("容器条码不能为空！");
        }
        if (com.cxyd.common.utils.StringUtils.isBlank(taskSubType)) {
            throw new RootException("任务子类型不能为空！");
        }
        List<TaskEntity> taskEntityList = taskService.taskQuery(contNo, taskSubType);
        if (null == taskEntityList || taskEntityList.size() == 0) {
            return new Result().error(ErrorCode.NOT_MATCH, "容器[" + contNo + "]没有出库任务！");
        }
        return new Result().ok(taskEntityList);
    }

    /**
     * 领料-出库完成
     */
    @PostMapping("/outbound/completed")
    public Result outboundCompleted(@RequestBody TaskRequest request) {
        if (null == request.getContNo()) {
            throw new RootException("托盘条码不能为空！");
        }
        if (null == request.getTaskId()) {
            throw new RootException("任务Id不能为空！");
        }
        if (null == request.getSpuNo()) {
            throw new RootException("物料编号不能为空！");
        }
        if (null == request.getLocateNo()) {
            throw new RootException("当前位置不能为空！");
        }
        if (null == request.getActlQty() || 0 == request.getActlQty()) {
            return stockService.outboundCompletedZero(request);
        }
        return stockService.outboundCompleted(request);
    }
}
